<?php
/**
* @author Bimal Poudel
* @copyright since 2006, Bimal Poudel
* @package Smarty Framework
* @support http://www.odesk.com/users/~~dd91d11caed0cdce
* @contact http://www.sanjaal.com
* @company Sanjaal Corps
*/

/**
* Login user authentication
*/

$session_message = isset($_SESSION['messenger'])?$_SESSION['messenger']:array();

# Which email address is being used?
$attempted_email = isset($_SESSION['attempted_email'])?$_SESSION['attempted_email']:'';
$attempted_email = isset($_GET['username'])?base64_decode($_GET['username']):$attempted_email;

$login = new login_manager();

$login_action = $variable->post('login-action', 'string', '');
if($login_action)
{
	$username = $variable->post('username', 'string', '');
	$password = $variable->post('password', 'string', '');

	$goto_page='./';
	if($login->login_user($username, $password))
	{
		if($goto = $variable->post('goto', 'string', ''))
		{
			$goto_page = $goto;
		}
		else if($goto = $variable->session('goto', 'string', ''))
		{
			# Unset it, and make it ready for other pages / login
			unset($_SESSION['goto']);
			$goto_page = $goto;
		}
		
		# Do not go to login form again, after a successful login
		# $goto_page=(strtolower($goto_page)!='login.php')?$goto_page:'./?rand='.tools::random_text(5);
		$goto_page='dashboard.php';
	}
	else
	{
		# Kick off the user and mention - login failed.
		$login->logout_user();
		$goto_page = 'login-failed.php';
	}
	stopper::url($goto_page);
}
else
{
	# Optionally, logout the use as well, as soon as the login page is requsted.
	$variable->kill('session', 'user_id');
}

# Continues to show the default login form.
if(empty($_SESSION['messenger'])) $_SESSION['messenger'] = $session_message;
?>